﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.WebApi.Media
{
    [AuthGetAction("media", "getList", "1.0")]
    public class GetListAction : BaseAction<GetListParams, GetListResult>
    {
        protected override ActionResult<GetListResult> OnExecute(ActionContext context, SysParams sysParams, GetListParams opParams)
        {
            CMS.Domain.Services.QueryService service = new Domain.Services.QueryService();
            CPPEI.PagedResult<CMS.Domain.Entities.EMedia> serResult = service.QueryMedias(opParams.FolderId, opParams.Page * 15, 15);

            if(serResult.IsSuccess)
            {
                GetListResult actResult = new GetListResult();
                foreach (CMS.Domain.Entities.EMedia media in serResult.Data)
                {
                    GetListResult.Item item = new GetListResult.Item();
                    item.Id = media.Id;
                    item.Url = string.Format("{0}{1}", CMS.Domain.Cache.BaseUrl, media.Url);

                    if (string.IsNullOrEmpty(media.Description))
                    {
                        item.Description = string.Empty;
                    }
                    else
                    {
                        item.Description = media.Description;
                    }
                    actResult.Items.Add(item);
                }
                return new ActionResult<GetListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetListResult>(serResult);
            }
        }
    }

    public class GetListParams
    {
        public int Page { get; set; }
        public Guid FolderId { get; set; }
    }

    public class GetListResult
    {
        public int Total { get; set; }
        public int Start { get; set; }
        public int Limit { get; set; }
        public int Page { get; set; }

        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }

            public string Url { get; set; }

            public string Description { get; set; }
        }
    }
}
